package com.qf.student.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.student.bo.CheckTimeBo;
import com.qf.student.dao.AdminMapper;
import com.qf.student.dao.CourseMapper;
import com.qf.student.dao.StudentMapper;
import com.qf.student.dao.TeacherMapper;
import com.qf.student.entity.Admin;
import com.qf.student.entity.Course;
import com.qf.student.entity.Student;
import com.qf.student.entity.Teacher;
import com.qf.student.response.R;
import com.qf.student.service.ICourseService;
import com.qf.student.vo.input.AdminCourseInputVo;
import com.qf.student.vo.input.PageSearchInputVo;
import com.qf.student.vo.output.AdminCourseOutputVo;
import com.qf.student.vo.output.ChooseCourseOutputVo;
import com.qf.student.vo.output.CourseTableOutputVo;
import com.qf.student.vo.output.TeacherScoreInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2022-11-21
 */
@Service
@Slf4j
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public R getCourseInfo(PageSearchInputVo inputVo) {
        //设置分页
        PageHelper.startPage(inputVo.getPageNum(), inputVo.getPageSize());

        //查询数据
        List<ChooseCourseOutputVo> courseList = courseMapper.getCourseInfo(inputVo);
//        for (ChooseCourseOutputVo chooseCourseOutputVo : courseList) {
//            log.debug(chooseCourseOutputVo.toString());
//        }
        //封装分页数据
        PageInfo<ChooseCourseOutputVo> info = new PageInfo<>(courseList);
        return R.createSuccess(info);
    }

    @Override
    public R getCourseTable(Integer userId) {
        Student student = studentMapper.selectById(userId);
        if (student == null) {
            return R.createFAIL("登录信息有误，请重新登录");
        }
        List<CourseTableOutputVo> courseTables = courseMapper.getCourseTable(userId);

        return R.createSuccess(courseTables);
    }

    @Override
    public R getTeacherCourse(Integer userId) {
        Teacher teacher = teacherMapper.selectById(userId);
        if (teacher == null) {
            return R.createFAIL("登录信息有误，请重新登录");
        }
        List<Course> courses = courseMapper.selectList(
                new QueryWrapper<Course>()
                        .eq("course_teacher_id", userId)
        );
        if (courses == null) {
            return R.createFAIL("未找到课程相关信息");
        }
        return R.createSuccess(courses);
    }

    @Override
    public R getTeacherCourseTable(Integer userId) {
        Teacher teacher = teacherMapper.selectById(userId);
        if (teacher == null) {
            return R.createFAIL("登录信息有误，请重新登录");
        }
        List<Course> courses = courseMapper.selectList(
                new QueryWrapper<Course>()
                        .eq("course_teacher_id", userId)
        );
        if (courses == null) {
            return R.createFAIL("未找到课表相关信息");
        }

        //使用 BeanUtils 复制属性，将第一个对象的属性复制给第二个属性
        List<CourseTableOutputVo> outputVos = new ArrayList<>();
        for (Course course : courses) {
            CourseTableOutputVo tableOutputVo = new CourseTableOutputVo();
            BeanUtils.copyProperties(course, tableOutputVo);
            outputVos.add(tableOutputVo);
        }
        return R.createSuccess(outputVos);
    }

    @Override
    public R getTeacherScoreInfo(PageSearchInputVo inputVo) {
        Teacher teacher = teacherMapper.selectById(inputVo.getId());
        if (teacher == null) {
            return R.createFAIL("登录信息有误，请重新登录");
        }

        //设置分页
        PageHelper.startPage(inputVo.getPageNum(), inputVo.getPageSize());

        List<TeacherScoreInfo> teacherScoreInfo = courseMapper.getTeacherScoreInfo(inputVo);

        //封装分页的数据
        PageInfo<TeacherScoreInfo> info = new PageInfo<>(teacherScoreInfo);
        return R.createSuccess(info);
    }

    @Override
    public R getAdminCourseInfo(Integer userId, AdminCourseInputVo inputVo) {
        Admin admin = adminMapper.selectById(userId);
        if (admin == null) {
            return R.createFAIL("登录信息有误，请重新登录");
        }
        PageHelper.startPage(inputVo.getPageNum(), inputVo.getPageSize());

        List<AdminCourseOutputVo> outputVoLIst = courseMapper.getAdminCourseInfo(inputVo);

        PageInfo<AdminCourseOutputVo> info = new PageInfo<>(outputVoLIst);

        return R.createSuccess(info);
    }

    @Override
    public R addCourseInfo(Course course, Integer userId) {
        Admin admin = adminMapper.selectById(userId);
        if (admin == null) {
            return R.createFAIL("登录信息有误，请重新登录");
        }

        //判断老师的上课时间是否冲突
        Integer teacherId = course.getCourseTeacherId();
        if (teacherId == null) {
            return R.createFAIL("所选的教师信息不存在");
        }
        List<Course> courses = courseMapper.selectList(
                new QueryWrapper<Course>()
                        .eq("course_teacher_id", teacherId)
        );
        String chooseCourseTime = course.getCourseTime();
        for (Course existsCourse : courses) {
            String existsCourseTime = existsCourse.getCourseTime();
            if (CheckTimeBo.timeConflict(existsCourseTime, chooseCourseTime)) {
                return R.createFAIL("Sorry，该老师的上课时间冲突了");
            }
        }

        if (courseMapper.insert(course) > 0) {
            return R.createSuccess("课程创建成功");
        }
        return R.createFAIL("课程添加失败");
    }

    /**
     * 删除课程的操作
     * @param courseId
     * @param userId
     * @return
     */
    @Override
    public R deleteCourse(Integer courseId, Integer userId) {
        Admin admin = adminMapper.selectById(userId);
        if (admin == null) {
            return R.createFAIL("登录信息有误，请重新登录");
        }

        Course course = courseMapper.selectById(courseId);
        if (course == null) {
            return R.createFAIL("请选择一个需要删除的课程");
        }
        if (courseMapper.deleteById(courseId) > 0) {
            return R.createSuccess("课程删除成功");
        }
        return R.createFAIL("课程删除失败");
    }

    @Override
    public R getCourseInfoById(Integer courseId, Integer userId) {
        Admin admin = adminMapper.selectById(userId);
        if (admin == null) {
            return R.createFAIL("登录信息有误，请重新登录");
        }

        Course course = courseMapper.selectById(courseId);
        if (course == null) {
            return R.createFAIL("未找到相关的课程信息");
        }

        return R.createSuccess(course);
    }

    @Override
    public R updateCourseInfo(Course course, Integer userId) {
        Admin admin = adminMapper.selectById(userId);
        if (admin == null) {
            return R.createFAIL("登录信息有误，请重新登录");
        }

        //判断老师的上课时间是否冲突
        Integer teacherId = course.getCourseTeacherId();
        if (teacherId == null) {
            return R.createFAIL("所选的教师信息不存在");
        }
        //修改操作，排除当前的修改的这个课程的冲突信息
        List<Course> courses = courseMapper.selectList(
                new QueryWrapper<Course>()
                        .eq("course_teacher_id", teacherId)
                        .ne("course_id", course.getCourseId())
        );

        if (courses != null) {
            String chooseCourseTime = course.getCourseTime();
            for (Course existsCourse : courses) {
                String existsCourseTime = existsCourse.getCourseTime();
                if (CheckTimeBo.timeConflict(existsCourseTime, chooseCourseTime)) {
                    return R.createFAIL("Sorry，该老师的上课时间冲突了");
                }
            }
        }

        if (courseMapper.updateById(course) > 0) {
            return R.createSuccess("课程修改成功");
        }
        return R.createFAIL("课程修改失败");
    }

}
